package com.ljm.boot.helloword.letCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test128 {
    //最长连续序列
    public static void main(String[] args) {
        int[]  nums  =new int[]{0,3,7,2,5,8,4,6,0,1};
        int count  = longestConsecutive(nums);
        System.out.println(count);
    }
    public  static int longestConsecutive(int[] nums) {
        if (nums.length == 0) return 0;
        List<Integer> list  =  new ArrayList<>();
        for (int num :nums) {
            if(list.contains(num)){
                continue;
            }
            list.add(num);
        }
        Collections.sort(list);
        if (list.size() == 1) return 1;
        int size = 1,count = 0;
        System.out.println(list.toString());
        for (int num : list) {
            if(list.contains(num+1)){
                size++;
            }else{
                if(size>count){
                    count = size;
                }
                size = 1;
            }
        }
        return count;
    }
    public static int longestConsecutiveLetCode(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return 1;
        Arrays.sort(nums);
        int ans = 1, sum = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == 0 || nums[i] - nums[i - 1] == 1) {
                if (nums[i] - nums[i - 1] == 0) {
                    sum--;
                }
                sum++;
            } else {
                sum = 1;
            }
            ans = Math.max(ans, sum);
        }
        return ans;
    }
}
